/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.videodes.desktop;

import cz.muni.fi.pb138.videodes.entity.Movie;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.AbstractListModel;

/**
 *
 * @author vaclav
 */
public class MovieListModel extends AbstractListModel {

    List<Movie> movies;
    
    public MovieListModel() {
        movies = new ArrayList<Movie>();
    }
    
    public MovieListModel(List<Movie> movies) {
        this.movies = movies;
        int row = this.movies.size();
        fireIntervalAdded(this, 0, row);
    }
    
    public void add(Movie movie) {
        movies.add(movie);
        
        int row = movies.size();
        fireIntervalAdded(this, row, row);
    }
    
    public void remove(Movie movie) {
        movies.remove(movie);
        int row = movies.size();
        fireIntervalRemoved(this, row, row);
    }
    
    public void update(Movie movie) {
        for (Movie m : movies) {
            if (m.equals(movie)) {
                m.setName(movie.getName());
                m.setDirector(movie.getDirector());
                m.setYear(movie.getYear());
                m.setCountry(movie.getCountry());
                m.setActors(movie.getActors());
                m.setGenres(movie.getGenres());
                int row = movies.size();
                
                fireContentsChanged(this, row-1, row-1);
                return;
            }
        }
    }
    
    public List<Movie> getAllMovies() {
        return Collections.unmodifiableList(movies);
    }

    @Override
    public int getSize() {
        return movies.size();
    }

    @Override
    public Object getElementAt(int i) {
        return movies.get(i);
    }
    
}
